// 继承及多态
using System;
namespace InheritSpace
{
    class InheritClass
    {
        static void Main(string[] args){
            /* 虚方法：virtual */
            class BaseClass
            {
                public virtual void DoSomething1()
                {
                    Console.WriteLine("BaseClass implementation");
                } 
            }
            class DerivedClass : BaseClass
            {
                public override void DoSomething1()
                {
                    Console.WriteLine("DerivedClass implementation1");
                } 
            }
            /* 类继承 */
            Rectangle Rect = new Rectangle(); 
            Rect.setWidth(5);
            Rect.setHeight(7);  
            Console.WriteLine("总面积： {0}", Rect.getArea());// 打印对象的面积
            /* 接口继承 */ 
            MyClass obj = new MyClass();  
            obj.Method1();// 调用继承自基接口的方法 
            obj.Method2();// 调用派生接口新增的方法
            /* 多重继承 */
            ManyCLass manyObj = new ManyCLass();
            manyObj.setWidth(5);
            manyObj.setHeight(5);
            Console.WriteLine("总面积： {0}", manyObj.getArea());// 打印对象的面积
            manyObj.Method1();
            /* 多态:通过继承实现不同对象调用相同方法表现不同的行为 */
            TestBook[] book = new TestBook[2];
            book[0] = new Abook();
            book[1] = new Cbook();
            for (int i =0;i<book.Length;i++)
            {
                book[i].Proint();
            }

        }
    }

    class Shape
    {
        public void setWidth(int w)
        {
            width = w;
        }
        public void setHeight(int h)
        {
            height = h;
        }
        protected int width;
        protected int height;
    }

    // 派生类
    class Rectangle : Shape
    {
        public int getArea()
        {
            return (width * height);
        }
    }
    // 定义一个基接口
    interface IBaseInterface
    {
        void Method1();
    }

    // 定义一个派生接口，继承自基接口
    interface IDerivedInterface : IBaseInterface
    {
        void Method2();
    }

    // 实现派生接口的类
    class MyClass : IDerivedInterface
    {
        public void Method1()
        {
            Console.WriteLine("Method1 implementation");
        }

        public void Method2()
        {
            Console.WriteLine("Method2 implementation");
        }
    }
    // C# 不支持类多重继承。但是，您可以使用接口来实现多重继承。
    class ManyCLass: Shape,IBaseInterface
    {
        public int getArea()
        {
            return (width * height);
        }
        public void Method1()
        {
            Console.WriteLine("Method1 implementation");
        }
    }
    /* 多态 */
    class TestBook
    {
        public virtual void Proint()
        {
            Console.WriteLine("基类");
        }
    }
    class Abook: TestBook
    {
        public override void Proint()
        {
            Console.WriteLine("子类A");
        }
    }
    class Cbook: TestBook
    {
        public override void Proint()
        {
            Console.WriteLine("子类C");
        }
    }

}
